package com.zw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.config.context.AuthenticationContextHolder;
import com.zw.entity.SysRole;
import com.zw.entity.SysUser;
import com.zw.mapper.SysMenuMapper;
import com.zw.mapper.SysRoleMapper;
import com.zw.mapper.SysUserMapper;
import com.zw.service.SysUserService;
import com.zw.utils.Constants;
import com.zw.utils.IdUtils;
import com.zw.utils.TokenUtils;
import com.zw.vo.LoginUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户信息表(SysUser)表服务实现类
 *
 * @author makejava
 * @since 2025-01-02 16:54:54
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private AuthenticationManager authenticationManager;
    @Resource
    private TokenUtils tokenUtils;
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;


    @Override
    public String login(String username, String password) {
        UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(username, password);
        AuthenticationContextHolder.setContext(authToken);
        // 使用authenticationManager调用loadUserByUsername获取数据库中的用户信息,
        Authentication authentication = authenticationManager.authenticate(authToken);
        if (authentication == null) {
            throw new RuntimeException("登录失败");
        }
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return tokenUtils.createToken(loginUser);
    }

    @Override
    public void validate(SysUser user) {
        Authentication usernamePasswordAuthenticationToken = AuthenticationContextHolder.getContext();
        String username = usernamePasswordAuthenticationToken.getName();
        String password = usernamePasswordAuthenticationToken.getCredentials().toString();
        //密码不正确抛出相应提示
        if (!password.equals(user.getPassword())) {
            throw new RuntimeException("密码错误");
        }
    }

    @Override
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        List<SysRole> roles = sysRoleMapper.selectRolesByUserId(user.getUserId());
        if (!roles.isEmpty() && roles.size() > 1) {
            // 多角色设置permissions属性，以便数据权限匹配权限
            for (SysRole role : roles) {
                Set<String> rolePerms = sysMenuMapper.selectMenuPermsByRoleId(role.getRoleId());
                perms.addAll(rolePerms);
            }
        } else {
            perms.addAll(sysMenuMapper.selectMenuPermsByUserId(user.getUserId()));
        }
        return perms;
    }

}
